import os
from urllib.request import urlopen
import struct

def readFile(file_path, output_path):
    data = []
    if file_path.startswith("http"):
        file = urlopen(file_path)
    else:
        file = open(file_path, 'rb')

    if file_path.split(".")[-1] == "txt":
        content = file.readlines()
        for con in content:
            data.append(str(con, 'utf-8').rstrip("\n"))
    elif file_path.split(".")[-1] in ["Single", "Decimal"]:
        content = file.read()
        for i in range(int(len(content) / 4)):
            b = "{:.16f}".format(struct.unpack('f', content[4 * i : 4 * (i + 1)])[0])
            data.append(b)
    file.close()

    # Write data to the output file
    with open(output_path, 'w') as f:
        for item in data:
            f.write("%s\n" % item)

def convert_all_files(input_directory, output_directory):
    for filename in os.listdir(input_directory):
        input_path = os.path.join(input_directory, filename)
        output_path = os.path.join(output_directory, filename.replace(".Single", ".txt").replace(".Decimal", ".txt"))
        readFile(input_path, output_path)

convert_all_files("F:\\code\\pythonProject\\11", "F:\\code\\pythonProject\\L1")
